package com.example.framework.picasso.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.framework.R;
import com.example.framework.picasso.Constants;
import com.squareup.picasso.Picasso;


public class PicassoListviewAdapter extends BaseAdapter
{
      
      private final Context mContext;
      
      public PicassoListviewAdapter(Context context)
      {
            mContext = context;
      }
      
      @Override
      public int getCount()
      {
            return Constants.IMAGES.length;
      }
      
      @Override
      public Object getItem(int position)
      {
            return null;
      }
      
      @Override
      public long getItemId(int position)
      {
            return 0;
      }
      
      @Override
      public View getView(int position, View convertView, ViewGroup parent)
      {
            ViewHolder holder;
            if (convertView == null)
            {
                  convertView = View.inflate(mContext, R.layout.item_picasso_listview, null);
                  
                  holder = new ViewHolder(convertView);
                  
                  convertView.setTag(holder);
            }
            else
            {
                  holder = (ViewHolder) convertView.getTag();
            }
            
            // 名称
            holder.name.setText("item" + (position + 1));
            
            // 加载图片
            Picasso.get().load(Constants.IMAGES[position]).placeholder(R.drawable.atguigu_logo).error(R.drawable.atguigu_logo).into(holder.iv);
            
            return convertView;
      }
      
      class ViewHolder
      {
            ImageView iv;
            TextView name;
            
            public ViewHolder(View view)
            {
                  iv = view.findViewById(R.id.iv_picasso_item);
                  name = view.findViewById(R.id.tv_picasso_name);
            }
      }
}
